<?php
namespace Matrix\Controller;

use Think\Model;
class DrawingsController extends CommonController {
	
public function __construct() {
		parent::__construct ();
		parent::__config ( array (
				'model_name'=>'Drawings',
		) );
	}
	
	public function index(){
		
		parent::__config ( array (
				'fuzzy' => 'code,username' ,
				'relation'=>true,
				'between_time'=>'review_time',
				'statistics'=>'money',//需要统计求和字段
		) );
		$_GET ['status'] = I ( 'status' );
// 		dump($_POST);
// 		die();
		$this->status=I ( 'status' );
		$table = I ( 'table' );
		if (! $table == null && ! empty ( $table )) {
			parent::__config ( array (
					'display' => 'table',
					'display_parms' => array (
							'table' => 1 
					) 
			) );
		}
		parent::index ();
	}
	
	// 审核通过
	function batch_summary() {
// 		$model = D ( "Model" );
// 		$model->startTrans ();
		// 获取订单套餐信息
		$where_find ['drawings_id'] = intval ( I ( 'id' ) );
		$where_find ['status'] = array ('eq',1);
		$drawingsModel = D ( "Drawings" );
		$drawings_info = $drawingsModel->where ( $where_find )->find ();
		if($drawings_info){
			$data_save['status']=2;
			$data_save['review_time']=time();
			$data_save['drawings_id']=$drawings_info['drawings_id'];
			$result_save = $drawingsModel->save($data_save);
			if($result_save){
				$this->redirect ( 'Drawings/index', array ('status' => 2) );
			}else{
				$this->redirect ( 'Drawings/index', array ('status' => 1) );
			}
		}
		$this->redirect ( 'Drawings/index', array ('status' => 1) );
	}
	
	// 批量审核通过
	function batch_summary_list() {
		$ids = $_REQUEST ['checkboxes'];
// 		dump($ids);
// 		die();
		foreach ( $ids as $key_ids => $value_ids ) {
		// 获取订单套餐信息
			$where_find ['drawings_id'] = intval ( $value_ids );
			$where_find ['status'] = array ('eq',1);
			$drawingsModel = D ( "Drawings" );
			$drawings_info = $drawingsModel->where ( $where_find )->find ();
			if($drawings_info){
				$data_save['status']=2;
				$data_save['review_time']=time();
				$data_save['drawings_id']=$drawings_info['drawings_id'];
				$result_save = $drawingsModel->save($data_save);
			}
		}
		$this->redirect ( 'Drawings/index', array ('status' => 2) );
	}
	
	
	// 打款成功
	function remit_success() {
		$where_find ['drawings_id'] = intval ( I ( 'id' ) );
		$where_find ['status'] = array ('eq',2);
		$drawingsModel = D ( "Drawings" );
		$drawings_info = $drawingsModel->where ( $where_find )->find ();
		if($drawings_info){
			$data_save['status']=3;
			$data_save['drawings_id']=$drawings_info['drawings_id'];
			$result_save = $drawingsModel->save($data_save);
			if($result_save){
				$this->redirect ( 'Drawings/index', array ('status' => 3) );
			}else{
				$this->redirect ( 'Drawings/index', array ('status' => 2) );
			}
		}
		$this->redirect ( 'Drawings/index', array ('status' => 2) );
	}
	
	// 打款失败
	function remit_fail() {
		$where_find ['drawings_id'] = intval ( I ( 'id' ) );
		$where_find ['status'] = array ('eq',2);
		$drawingsModel = D ( "Drawings" );
		$drawings_info = $drawingsModel->where ( $where_find )->find ();
		if($drawings_info){
			$data_save['status']=4;
			$data_save['drawings_id']=$drawings_info['drawings_id'];
			$result_save = $drawingsModel->save($data_save);
			if($result_save){
				$this->redirect ( 'Drawings/index', array ('status' => 4) );
			}else{
				$this->redirect ( 'Drawings/index', array ('status' => 2) );
			}
		}
		$this->redirect ( 'Drawings/index', array ('status' => 2) );
	}
	
	
	//退回余额
	function back_to_account(){
		
		//开启事务
		$commint=true;//判断是否需要回滚
		$model = new \Think\Model ();
		$model->startTrans ();
		
		$where_find ['drawings_id'] = intval ( I ( 'id' ) );
		$where_find ['status'] = array ('eq',4);
		$drawingsModel = D ( "Drawings" );
		$drawings_info = $drawingsModel->where ( $where_find )->find ();
		$data_save['status']=5;
		$data_save['drawings_id']=$drawings_info['drawings_id'];
		$result_save = $drawingsModel->save($data_save);
		$agentModel = D("agent");
		$where_agent['agent_id']=$drawings_info['agent_id'];
		$result_inc = $agentModel->where($where_agent)->setInc('money',$drawings_info['money']);
	
		if($result_save&&$result_inc){
			$model->commit ();
		}else{
			$model->rollback ();
		}
		$this->redirect ( 'Drawings/index', array ('status' =>5) );
	}
	
	function add() {
	}

	function foreverdelete() {

	}
	
	function edit() {
		parent::__config ( array (
				'display'=>'index',
		) );
		$data['id']=I('id');
		$data['status']=1;
		parent::update ($data);
// 		parent::edit ();
	}
	
	function update() {
		parent::__config ( array (
				'display'=>'index',
		) );
		$data['id']=I('id');
		$data['status']=0;
		parent::update ($data);
	}
	function logout(){
		session("ssc_agent",null);
		session('agent',null);
		$this->redirect('Home/Index/index');
	}
	
	function bean() {
		parent::bean();
	}
	
	function sort() {
		parent::sort ();
	}
	
	function excelImport(){
	}
	
	function excelOutput(){
		
	}
	
}